import { request } from '../../../services/_utils/util';
const api = require('../../../services/config/api');
import Notify from '@vant/weapp/notify/notify';

Page({
  data: {
    id: 0,
    nameValue: '',
    phoneNum: '',
    newphoneNum: '',
    isname: false,
    isphone: false,
    formdata: {},
    display: 'none',
    display2: 'inline-black',
    display3: 'none',
    display4: 'inline-black',
    seconds: 59,
    seconds2: 59,
  },
  onLoad(options) {
    console.log(options);
    if (options.isname == 'true') {
      this.setData({
        nameValue: options.name,
        isname: options.isname,
        isphone: false,
        id: options.id,
      });
    } else if (options.isphone == 'true') {
      this.setData({
        phoneNum: options.phone,
        isphone: options.isphone,
        isname: false,
        id: options.id,
      });
      console.log(this.data.phoneNum);
    }
  },
  onShow() {
    console.log(wx.getStorageSync('userInfo'));
    this.setData({
      nameValue: wx.getStorageSync('userInfo').name,
      phoneNum: wx.getStorageSync('userInfo').phone,
    });
  },
  onSubmit() {
    var that = this;
    request(api.updateUser, { id: this.data.id, name: this.data.nameValue }, 'post').then(function (res) {
      if (res.code == 200) {
        Notify({ type: 'success', message: '修改成功' });
        var user = wx.getStorageSync('userInfo');
        user.name = that.data.nameValue;
        wx.setStorageSync('userInfo', user);
      } else {
        Notify({ type: 'success', message: res.msg });
      }
    });
  },
  clearContent() {
    this.setData({
      nameValue: '',
    });
  },
  timeChange(e) {
    //倒计时改变
    this.setData({
      seconds: e.detail.seconds,
    });
  },
  timeFinish(e) {
    //倒计时结束
    const countDown = this.selectComponent('.control-count-down');
    countDown.reset();
    this.setData({
      display: 'none',
      display2: 'inline-black',
    });
  },
  timeChange2(e) {
    //倒计时改变
    this.setData({
      seconds2: e.detail.seconds,
    });
    console.log(this.data.seconds2);
  },
  timeFinish2(e) {
    //倒计时结束
    const countDown = this.selectComponent('.control-count-down2');
    countDown.reset();
    this.setData({
      display3: 'none',
      display4: 'inline-black',
    });
  },
  //获取验证码
  getYzm() {
    // var that = this;
    // that.setData({
    //   display: 'inline-black',
    //   display2: 'none',
    // });
    // const countDown = that.selectComponent('.control-count-down');
    // countDown.start();

    if (this.data.phoneNum == null || this.data.phoneNum.trim() == '' || this.data.phoneNum == undefined) {
      Notify({ type: 'danger', message: '系统繁忙,未检测出已绑定的手机号' });
      return false;
    }

    request(api.smsyzm + '?phone=' + this.data.phoneNum, {}, 'post').then(function (res) {
      console.log(res);
      if (res.code == 200) {
        Notify({ type: 'success', message: '发送成功' });
        that.setData({
          display: 'inline-black',
          display2: 'none',
        });
        var countDown = that.selectComponent('.control-count-down');
        countDown.start();
      } else {
        Notify({ type: 'danger', message: res.msg });
      }
    });
  },
  //获取验证码
  getYzm2() {
    // var that = this;
    // that.setData({
    //   display3: 'inline-black',
    //   display4: 'none',
    // });
    // var countDown = that.selectComponent('.control-count-down2');
    // countDown.start();

    if (this.data.newphoneNum == null || this.data.newphoneNum.trim() == '' || this.data.newphoneNum == undefined) {
      Notify({ type: 'danger', message: '请填写手机号' });
      return false;
    }

    request(api.smsyzm + '?phone=' + this.data.newphoneNum, {}, 'post').then(function (res) {
      console.log(res);
      if (res.code == 200) {
        Notify({ type: 'success', message: '发送成功' });
        that.setData({
          display3: 'inline-black',
          display4: 'none',
        });
        const countDown = that.selectComponent('.control-count-down2');
        countDown.start();
      } else {
        Notify({ type: 'danger', message: res.msg });
      }
    });
  },
  onSubmit2() {
    console.log(this.data.formdata);
    var that = this;
    if (
      this.data.formdata.yzmcode == null ||
      this.data.formdata.yzmcode.trim() == '' ||
      this.data.formdata.yzmcode == undefined
    ) {
      Notify({ type: 'danger', message: '请填写验证码' });
      return false;
    }
    if (
      this.data.formdata.yzmcode2 == null ||
      this.data.formdata.yzmcode2.trim() == '' ||
      this.data.formdata.yzmcode2 == undefined
    ) {
      Notify({ type: 'danger', message: '请填写验证码' });
      return false;
    }
    this.setData({
      newphone: this.data.newphoneNum,
      phone: this.data.phoneNum,
      id: this.data.id,
    });
    request(api.updateUser, this.data.formdata, 'post').then(function (res) {
      if (res.code == 200) {
        Notify({ type: 'danger', message: '修改成功' });
        //更新数据
        var user = wx.getStorageSync('userInfo');
        user.phone = that.data.newphoneNum;
        wx.setStorageSync('userInfo', user);
        that.setData({
          phoneNum: that.data.newphoneNum,
        });
      } else {
        Notify({ type: 'danger', message: res.msg });
      }
    });
  },
});
